#include <amxmodx>
#include <cstrike>
#include <engine>
#include <items>

new const PLUGIN[]  = "Jetpack";
new const VERSION[] = "1.0";
new const AUTHOR[]  = "Liquidair";

new const CVAR_JP_MAX[]    = "jp_max";
new const CVAR_JP_SPEED[]  = "jp_speed";
new const CVAR_JP_TRAIL[]  = "jp_trail";

new JP_SOUND[] = "jetpack.wav";

new flame , smoke;
new bool:g_jetpack[33];
new frame[33];
new current_jps;

public plugin_init() {
	register_plugin(PLUGIN , VERSION , AUTHOR);
	register_item("Jetpack", "Fly in the air", 2500)

	register_cvar(CVAR_JP_MAX      , "4");
	register_cvar(CVAR_JP_SPEED    , "500");
	register_cvar(CVAR_JP_TRAIL    , "1");
}

public client_connect(id) {
	g_jetpack[id] = false;
}

public Enable_Item(id) {
	g_jetpack[id] = true
}

public Disable_Item(id) {
	g_jetpack[id] = false
}

public plugin_precache() {
	smoke = precache_model("sprites/lightsmoke.spr");
	flame = precache_model("sprites/rjet1.spr"); // bm1, rjet1, stmbal1, WXplo1, xsmoke1, lightsmoke
	precache_model("models/w_longjump.mdl");
	precache_sound(JP_SOUND);
}

public client_PreThink(id) {
	if(!is_user_connected(id) || !is_user_alive(id)) {
		return PLUGIN_CONTINUE;
	}
	if(!(get_user_button(id) & IN_JUMP)) {
		return PLUGIN_CONTINUE;
	}
	if(!g_jetpack[id]) {

		return PLUGIN_CONTINUE;
	}
	new Float:fAim[3] , Float:fVelocity[3];
	VelocityByAim(id , get_cvar_num(CVAR_JP_SPEED) , fAim);

	fVelocity[0] = fAim[0];
	fVelocity[1] = fAim[1];
	fVelocity[2] = fAim[2];

	set_user_velocity(id , fVelocity);

	entity_set_int(id , EV_INT_gaitsequence , 6);

	emit_sound(id , CHAN_VOICE , JP_SOUND , 1.0 , ATTN_NORM , 0 , PITCH_NORM);

	if(frame[id] >= 3) {
		frame[id] = 0;
		if(get_cvar_num(CVAR_JP_TRAIL)) {
			smoke_effect(id);
		}
		entity_set_string(id , EV_SZ_weaponmodel , "models/w_longjump.mdl");
	}
	frame[id]++;

	return PLUGIN_CONTINUE;
}

public smoke_effect(id) {
	new origin[3];
	get_user_origin(id, origin, 0);
	origin[2] = origin[2] - 10;

	message_begin(MSG_BROADCAST, SVC_TEMPENTITY);
	write_byte(17);
	write_coord(origin[0]);
	write_coord(origin[1]);
	write_coord(origin[2]);
	write_short((get_cvar_num(CVAR_JP_TRAIL) == 1) ? smoke : flame);
	write_byte(10);
	write_byte(115);
	message_end();
}

public ClCmd_BuyJP(id) {
	
	if(g_jetpack[id]) {
		client_print(id , print_chat , "[AMXX] You already have a jetpack!");
		return PLUGIN_HANDLED;
	}
	
	g_jetpack[id] = true;

	client_print(id , print_chat , "[AMXX] To use the jetpack hold jump and aim in the direction you wish to move in");
	//client_print(id , print_chat , "[AMXX] To drop it, say /dropjp in chat");

	emit_sound(id , CHAN_VOICE , "items/gunpickup2.wav" , 1.0 , ATTN_NORM , 0 , PITCH_NORM);

	current_jps++;

	return PLUGIN_HANDLED;
}

/*
public drop_jp(id) {
	if(!g_jetpack[id]) return PLUGIN_HANDLED;

	new Float:fAim[3] , Float:fOrigin[3];
	VelocityByAim(id , 64 , fAim);
	entity_get_vector(id , EV_VEC_origin , fOrigin);

	fOrigin[0] += fAim[0];
	fOrigin[1] += fAim[1];

	new jp = create_entity("info_target");

	entity_set_string(jp , EV_SZ_classname , "jetpack");
	entity_set_model(jp , "models/w_longjump.mdl");

	entity_set_size(jp , Float:{-16.0,-16.0,-16.0} , Float:{16.0,16.0,16.0});

	entity_set_int(jp , EV_INT_solid , 1);
	entity_set_int(jp , EV_INT_movetype , 6);

	entity_set_vector(jp , EV_VEC_origin , fOrigin);

	g_jetpack[id] = false;

	return PLUGIN_HANDLED;
}
*/
